//
//  FirstViewController.m
//  UI - 豆瓣
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "FirstViewController.h"
#import "FirstViewCell.h"
#import "AppTools.h"
#import "Activety.h"
#import "ActivetyDataViewController.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"



@interface FirstViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *myTableView;
@property(nonatomic, retain)NSMutableArray *bigArray;
@property(nonatomic, retain)MBProgressHUD *hub;
@property(nonatomic, copy)NSString *activetyFilePath;

@end

@implementation FirstViewController
- (void)dealloc
{
    [_hub release];
    [_bigArray release];
    [_myTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"活动";
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    [self.myTableView registerClass:[FirstViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    self.bigArray = [NSMutableArray array];
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    self.activetyFilePath = [document stringByAppendingPathComponent:@"actevity.xml"];
    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:self.activetyFilePath];
    for (NSMutableDictionary *dic in array) {
        Activety *activety = [[Activety alloc] init];
        [activety setValuesForKeysWithDictionary:dic];
        [self.bigArray addObject:activety];
        [activety release];
    }
    
    [self addHeaderRefresh];
    
}

-(void)addHeaderRefresh{
    [self.myTableView addHeaderWithCallback:^{

    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"@拜着急@";
    [AppTools getWithName:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php" Block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"events"];
        [NSKeyedArchiver archiveRootObject:array toFile:self.activetyFilePath];
        
        for (NSMutableDictionary *key in array) {
            Activety *activety = [[Activety alloc] init];
            [activety setValuesForKeysWithDictionary:key];
            [self.bigArray addObject:activety];
            [activety release];
        }
        [self.myTableView headerEndRefreshing];
        [self.myTableView reloadData];
        [self.hub hide:YES];
    }];
    }];
    [self.myTableView headerBeginRefreshing];

}
-(UITableView *)myTableView{

    if (_myTableView == nil) {
        self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
        self.myTableView.delegate = self;
        self.myTableView.dataSource = self;
        self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _myTableView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.bigArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
   static NSString *fildPath = @"cell";
    FirstViewCell *cell = [tableView dequeueReusableCellWithIdentifier:fildPath];
    
    Activety *activety = [self.bigArray objectAtIndex:indexPath.row];
    cell.activety = activety;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ActivetyDataViewController *activetyDataVC = [[ActivetyDataViewController alloc] init];
    Activety *activety = [self.bigArray objectAtIndex:indexPath.row];
    activetyDataVC.activety = activety;
    
    [self.navigationController pushViewController:activetyDataVC animated:YES];
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 185;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
